/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.tween.sprop {
	import flash.display.DisplayObject;
	import flash.filters.BevelFilter;
	import flash.filters.BitmapFilter;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.ConvolutionFilter;
	import flash.filters.DisplacementMapFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.filters.GradientBevelFilter;
	import flash.filters.GradientGlowFilter;

	/**
	 * Enables the tweening of DisplayObject filter properties
	 * 
	 * @author Max Kugland
	 */
	public class FilterProp implements ISpecialProp {
		public static const BLUR_X : String = "blurX";
		public static const BLUR_Y : String = "blurY";
		public static const STRENGTH : String = "strength";
		public static const DISTANCE : String = "distance";

		private var _filter : BitmapFilter;

		/**
		 * The <code>BitmapFilter</code> of the target DisplayObject whose properties are to be 
		 * tweened 
		 */
		public function FilterProp(filter : BitmapFilter) {
			_filter = filter;
		}

		/**
		 * @inheritDoc
		 */
		public function apply(target : Object, prop : String, start : Number, end : Number, value : Number, percent : Number) : void {
			var t : DisplayObject = DisplayObject(target);
			var filters : Array = t.filters;
			
			
			//TODO why not just var i : int = getFilterIndex(_filter);
			for (var i : int = 0;i < filters.length; i++) {
				if(getFilterType(BitmapFilter(filters[i])) == getFilterType(_filter)) {
					break;
				}
			}
				
			var copyAr : Array = filters.concat();
			_filter[prop] = value;
			copyAr[i] = _filter;
			t.filters = copyAr;
		}

		
		/**
		 * @inheritDoc
		 */
		public function getValue(prop : String) : Number {
			return _filter[prop];
		}

		private function getFilterType(filter : BitmapFilter) : String {
			if(filter is BevelFilter) return "BevelFilter";
			else if(filter is BlurFilter) return "BlurFilter";
			else if(filter is ColorMatrixFilter) return "ColorMatrixFilter";
			else if(filter is ConvolutionFilter) return "ConvolutionFilter";
			else if(filter is DisplacementMapFilter) return "DisplacementMapFilter";
			else if(filter is DropShadowFilter) return "DropShadowFilter";
			else if(filter is GlowFilter) return "GlowFilter";
			else if(filter is GradientBevelFilter) return "GradientBevelFilter";
			else if(filter is GradientGlowFilter) return "GradientGlowFilter";
			else return null;
		}
	}
}
